<template>
  <el-card>
    <h2>Docker信息管理</h2>

    <!-- 查询表单 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="docker服务器地址">
        <el-input v-model="queryForm.ip" placeholder="请输入主机地址" clearable></el-input>
      </el-form-item>
      <el-form-item label="docker服务器名称">
        <el-input v-model="queryForm.name" placeholder="请输入名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="success" @click="openDialog">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="ip" label="主机地址"></el-table-column>
      <el-table-column prop="port" label="端口"></el-table-column>
      <el-table-column label="操作" width="220">
        <template #default="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!-- 添加/编辑对话框 -->
    <docker-form-dialog ref="dockerFormDialog" @refresh="fetchData"></docker-form-dialog>
  </el-card>
</template>

<script>
import axios from 'axios'
import DockerFormDialog from './DockerFormDialog.vue'

export default {
  components: {
    DockerFormDialog
  },
  data() {
    return {
      queryForm: {
        ip: '',
        name: '',
        page: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0
    }
  },
  methods: {
    fetchData() {
      axios.get('/pidscloud/docker/list', {
        params: this.queryForm
      }).then(response => {
        const { list, total, page, pageSize } = response.data.data
        this.tableData = list
        this.total = total
        this.queryForm.page = page
        this.queryForm.pageSize = pageSize
      }).catch(error => {
        console.error('获取Docker信息失败:', error)
      })
    },
    handleSizeChange(val) {
      this.queryForm.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.page = val
      this.fetchData()
    },
    onQuery() {
      this.queryForm.page = 1
      this.fetchData()
    },
    resetQuery() {
      this.queryForm = {
        ip: '',
        name: '',
        page: 1,
        pageSize: 10
      }
      this.fetchData()
    },
    openDialog() {
      this.$refs.dockerFormDialog.openDialog()
    },
    handleEdit(row) {
      this.$refs.dockerFormDialog.openDialog(row)
    },
    handleDelete(row) {
      this.$confirm('确定删除该Docker信息吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.delete(`/pidscloud/docker/delete/${row.id}`)
          .then(() => {
            this.$message.success('删除成功')
            this.fetchData()
          })
          .catch(error => {
            console.error('删除失败:', error)
          })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  },
  mounted() {
    this.fetchData()
  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 20px;
}
.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style> 